package com.i8office.android.zhihuixinwu.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.cyan.tools.annotation.ActivityFragmentInject;
import com.cyan.tools.base.BaseFragment;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.gyf.barlibrary.ImmersionBar;
import com.i8office.android.zhihuixinwu.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


@ActivityFragmentInject(
        contentViewId = R.layout.fragment_leader_schedule
)
public class LeaderScheduleHomeFragment extends BaseFragment {
    @BindView(R.id.tabLayout)
    SegmentTabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    String titles[] = {"按领导查看", "按日期查看"};
    private List<BaseRefreshFragment> fragments = new ArrayList<>();

    public static LeaderScheduleHomeFragment newInstance() {
        Bundle args = new Bundle();
        LeaderScheduleHomeFragment fragment = new LeaderScheduleHomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView(View view) {
        ButterKnife.bind(this, view);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f) //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                .init();
        fragments.add(LeaderScheduleByNameFragment.newInstance());
        fragments.add(LeaderScheduleByDateFragment.newInstance());
        viewpager.setOffscreenPageLimit(2);
        initViewPager();

    }

    //获取Tab
    private void initViewPager() {
        tabLayout.setTabData(titles);
        tabLayout.setVisibility(View.VISIBLE);
        viewpager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }

            @Override
            public int getCount() {
                return titles.length;
            }
        });
        setupWithViewPager();
    }

    private void setupWithViewPager() {
        tabLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewpager.setCurrentItem(position);
                fragments.get(position).refresh();
            }

            @Override
            public void onTabReselect(int position) {
                fragments.get(position).refresh();
            }
        });

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
